﻿using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using YTCMS.Core;
using YTCMS.Services.Services;
using System.Text;

namespace App.Common
{
    public class T
    {
        public static string Convert(string str)
        {
            System.Resources.ResourceManager rs = new System.Resources.ResourceManager("App.ErrorMessages", typeof(ErrorMessages).Assembly);
            var title = rs.GetString(str);
            return title;
        }
        private static IUnityContainer GetContainer()
        {
            IUnityContainer container = new UnityContainer();
            string file = HttpContext.Current.Server.MapPath("~/config/Unity.config");
            var fileMap = new ExeConfigurationFileMap() { ExeConfigFilename = file };
            var config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            var configuration = (UnityConfigurationSection)config.GetSection(UnityConfigurationSection.SectionName);
            configuration.Configure(container, "defaultContainer");
            return container;
        }

        public static string[] GetImages(int pid,Images type)
        {

            IUnityContainer container = GetContainer();
            imagesService images = container.Resolve<imagesService>() as imagesService;
            var i = images.GetMany(s => s.Pid == pid && s.Type == (int)type);
            if (i == null)
                return new string[0];
            else {
                return i.Select(s => s.Url).ToArray();
            }
        }
        public static string GetImagesByString(int pid, Images type)
        {

            IUnityContainer container = GetContainer();
            imagesService images = container.Resolve<imagesService>() as imagesService;
            var i = images.GetMany(s => s.Pid == pid && s.Type == (int)type);
            if (i == null)
                return "";
            else
            {
                StringBuilder sb = new StringBuilder();
                foreach (var item in i)
                {
                    sb.Append(item.Url +",");
                }
                if (sb.Length > 0)
                    return sb.Remove(sb.Length - 1, 1).ToString();
                return "";
            }
        }
    }
}